package com.groupbooks.server;

import java.io.IOException;
import java.net.URL;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gdata.client.books.BooksService;
import com.google.gdata.data.books.BooksLink;
import com.google.gdata.data.books.VolumeEntry;
import com.google.gdata.data.books.VolumeFeed;
import com.google.gdata.data.dublincore.Creator;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.groupbooks.client.GreetingService;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements
		GreetingService {

	/**
	 * The name of the server hosting the YouTube GDATA feeds.
	 */
	public static final String BOOKS_GDATA_SERVER = "http://books.google.com";

	/**
	 * The URL of the volumes feed
	 */
	public static final String VOLUMES_FEED = BOOKS_GDATA_SERVER
			+ "/books/feeds/volumes";

	/**
	 * The URL of the user library feeds
	 */
	public static final String USER_LIBRARY_FEED = BOOKS_GDATA_SERVER
			+ "/books/feeds/users/me/collections/library/volumes";

	public String loginUser(){
    	UserService userService = UserServiceFactory.getUserService();
		
		String thisURL = this.getThreadLocalRequest().getRequestURI();
		String logAction = "";
		
		if(!userService.isUserLoggedIn()){
			userService.createLoginURL(this.getThreadLocalRequest().getRequestURI());
			logAction = "<p>Please <a href=\"" +
				userService.createLoginURL(thisURL) +
				"\">sign in</a>.</p>";
		}
		else {
			logAction = "<p>Hello, " +
			this.getThreadLocalRequest().getUserPrincipal().getName() +
		        "!  You can <a href=\"" +
		        userService.createLogoutURL(thisURL) +
		        "\">sign out</a>.</p>";
		}
		return logAction;
	}

	/**
	 * Print a volume feed of entries as a numbered list
	 * 
	 * @param volumeFeed
	 *            A feed of volumes
	 * @throws IOException
	 *             Error sending request or reading the feed.
	 * @throws ServiceException
	 *             If the service is unable to handle the request.
	 */
	private static String printVolumeFeed(VolumeFeed volumeFeed)
			throws IOException, ServiceException {
		List<VolumeEntry> volumeEntries = volumeFeed.getEntries();
		if (volumeEntries.size() == 0) {
			return "This feed contains no entries.";
		}

		StringBuilder strBuilder = new StringBuilder();
		strBuilder.append("Feed Title: " + volumeFeed.getTitle().getPlainText()
				+ "<br>");
		for (VolumeEntry entry : volumeEntries) {
			strBuilder.append("<br>");
			strBuilder.append(printVolumeEntry(entry));
			strBuilder.append("<br>");
		}

		return strBuilder.toString();
	}

	/**
	 * Prints a VolumeEntry
	 * 
	 * @param entry
	 *            The VolumeEntry to be printed
	 * @throws IOException
	 *             Error sending request or reading the feed.
	 * @throws ServiceException
	 *             If the service is unable to handle the request.
	 */
	private static String printVolumeEntry(VolumeEntry entry)
			throws IOException, ServiceException {
		StringBuilder strBuilder = new StringBuilder();
		strBuilder.append("Title: " + entry.getTitle().getPlainText());
		strBuilder.append("<br>");

		for (Creator creator : entry.getCreators()) {
			strBuilder.append("Author: " + creator.getValue());
			strBuilder.append("<br>");
		}

		if (entry.hasRating()) {
			strBuilder.append("Rating: " + entry.getRating().getValue());
			strBuilder.append("<br>");
		}
		if (entry.hasReview()) {
			strBuilder.append("Review: " + entry.getReview().getValue());
			strBuilder.append("<br>");
		}

		strBuilder.append("<img src='"
				+ entry.getLink(null, BooksLink.Type.ANY_IMAGE).getHref()
				+ "'/>");
		
		strBuilder.append("   <a href='" + entry.getHtmlLink().getHref() + "'>");
		strBuilder.append("Link to item</a>");


		return strBuilder.toString();
	}
}
